class request {
    constructor() {
        this._header = {}
    }

    /**
     * 设置统一的异常处理
     */
    setErrorHandler(handler) {
        this._errorHandler = handler;
    }

    /**
     * GET类型的网络请求
     */
    getRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'GET')
    }

    /**
     * DELETE类型的网络请求
     */
    deleteRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'DELETE')
    }

    /**
     * PUT类型的网络请求
     */
    putRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'PUT')
    }

    /**
     * POST类型的网络请求
     */
    postRequest(url, data, header = this._header) {
        return this.requestAll(url, data, header, 'POST')
    }

    // 封装请求
    requestAll(url, data, header, method) {
        return new Promise((resolve, reject) => {
            // 调用请求显示loading
            wx.showLoading({
                title: '加载中...'
            })

            wx.request({
                url: url,
                data: data,
                header: header,
                method: method,
                success: (res => {
                    // 关闭loading
                    wx.hideLoading()  
                    if (res.statusCode === 200) {
                        // 如果token过期，需要重新调用getopenid
                        if (res.data.errorCode == '401') {
                            wx.removeStorage({
                              key: 'token',
                            })
                            wx.login({
                                success: res => { 
                                    wx.Http.post("/UserAccountAppService/Xncf.Xxgmall_UserAccountAppService.GetOpenId", {
                                        code: res.code,
                                    }).then((res) => {
                                        if (res.success == true) {
                                            console.log("测试第一个地点")
                                            // wx.role= res.data.value.role
                                            wx.setStorageSync('userId', res.data.value.userId)
                                            wx.setStorageSync("token", res.data.value.token);
                                        //     this.sequentialAPIs();
                                        // } else this.stopHeartbeat();
                                        }
                                    });
                                }
                            });
                            wx.showToast({
                                title: '服务器超时，请重试',
                                icon: 'none'
                            })
                        } else {
                            resolve(res)
                        }

                    } else {
                        if (res.statusCode == 401) {
                                wx.login({
                                    success: (res) => {
                                        wx.removeStorage({
                                            key: 'token',
                                          })
                                        if (res.errMsg == "login:ok") {
                                            wx.Http.post("/UserAccountAppService/Xncf.Xxgmall_UserAccountAppService.GetOpenId", {
                                                code: res.code,
                                            }).then((res) => {
                                                if (res.success == true) {
                                                    console.log("测试第二个地点")
                                                    // wx.role= res.data.value.role
                                                    wx.setStorageSync('userId', res.data.value.userId)
                                                    wx.setStorageSync("token", res.data.value.token);
                                                //     this.sequentialAPIs();
                                                // } else this.stopHeartbeat();
                                                }
                                            });
                                        }
                                    },
                                })

                        }
                        //其它错误，提示用户错误信息
                        if (this._errorHandler != null) {
                            //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理
                            this._errorHandler(res)
                        }
                        reject(res)
                    }
                }),
                fail: (res => {
                    // 关闭loading
                    wx.hideLoading()
                    if (this._errorHandler != null) {
                        this._errorHandler(res)
                    }
                    reject(res)
                })
            })
        })
    }
}

export default request